package jcataloger.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.filechooser.FileSystemView;

import jcataloger.beans.DiskImageInfo;
import jcataloger.beans.ImageItem;
import jcataloger.beans.SearchResultBean;
import jcataloger.gui.JCatalogerParams;
import jcataloger.resources.Parameters;

/**
 * Contains static utility methods of the project.
 * @author sekobey
 *
 */
public class Utils {

	/**
	 * controls if the the given disk name exists in the image names.
	 * @param imageName the given disk name
	 * @param diskImageList contains the images in the catalog
	 * @return
	 */
	public static boolean isDiskExist(String imageName, List<DiskImageInfo> diskImageList) {
		boolean result = false;
		for ( DiskImageInfo item : diskImageList ) {
			if ( imageName.equals( item.getDiskName() ) ) {
				result = true;
				break;
			}
		}
		return result;
	}

	/**
	 * Finds a disk image in the images of the catalog 
	 * @param imageName the name of the disk to be searched
	 * @param diskImageList the images in the catalog
	 * @return the found disk image information
	 */
	public static DiskImageInfo findDiskImage(String imageName, List<DiskImageInfo> diskImageList) {
		for ( DiskImageInfo item : diskImageList ) {
			if ( item.getDiskName().equals( imageName ) )
				return item;
		}
		return null;
	}

	/**
	 * Creates a file with the default extension
	 * @param file the file to be added an extension
	 * @return
	 */
	public static File createExtension(File file) {
		String name = file.getName();
		if (name.lastIndexOf(".") == -1 ) {
			name += "."+JCatalogerParams.FILE_EXTENSION;
		}
		else {
			String ext = name.substring(name.lastIndexOf(".")+1);
			if ( !ext.equals(JCatalogerParams.FILE_EXTENSION) )
				name += "."+JCatalogerParams.FILE_EXTENSION;
		}
		if (name.equals(file.getName()))
			return file;
		String path = file.getPath().substring(0, file.getPath().lastIndexOf(File.separator));
		return new File(path+File.separator+name);
	}
	
	/**
	 * Removes a disk image from the catalog.
	 * @param imageName the name of the image to be removed
	 * @param diskImageList the images in the catalog
	 */
	public static void removeDiskImageFromList(String imageName, List<DiskImageInfo> diskImageList) {
		Iterator i = diskImageList.iterator();
		while ( i.hasNext() ) {
			DiskImageInfo info = (DiskImageInfo)i.next();
			if ( info.getDiskName().equals( imageName ) ) {
				i.remove();
				break;
			}
		}
		
	}

	/**
	 * Controls if the given text is empty
	 * @param text
	 * @return
	 */
	public static boolean isTextEmpty(String text) {
		return text.equals("");
	}
	
	/**
	 * Windows and Linux has different file system structure. This method returns the linux <br>
	 * root directories if the system is linux.
	 * @param roots
	 * @return
	 */
	public static File[] getLinuxRoots(File[] roots) {
		String[] linuxRoots = Parameters.getString("linuxroots").split(",");
		ArrayList<File> list = new ArrayList<File>();
		for ( int i=0; i<roots.length; i++ )
			list.add( roots[i] );
		if ( System.getProperty("os.name").toLowerCase().indexOf("linux") > -1) {
			FileSystemView fsView = FileSystemView.getFileSystemView();
			for (String root : linuxRoots ) {
				File f = new File(root);
				File[] files = fsView.getFiles(f, true);
				for (int i = 0; i < files.length; i++) {
					list.add(files[i]);
				}
			}
		}
		
		return list.toArray(roots);
	}

}
